"""
auth:xgt-python
datetime:2021/11/14
爬取直聘网招聘信息
"""
# 导入相应的模块
"""
爬取Boss直聘网招聘信息
"""
# 导入相应的模块
import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import csv

with open(f'boss_{datetime.date.today()}.csv', 'a+', encoding='utf-8', newline='') as fp:
    csv_writer = csv.DictWriter(fp, fieldnames=[
        '岗位名称',
        '公司名称',
        '薪资',
        '工作地点',
        '公司类型',
        '详情页链接',
    ])
    csv_writer.writeheader()

    driver = webdriver.Chrome()
    driver.get('https://www.zhipin.com/shijiazhuang/?ka=header-home')
    driver.implicitly_wait(15)
    driver.find_element(By.CLASS_NAME, 'ipt-search').send_keys('人工智能')
    driver.find_element(By.CLASS_NAME, 'btn-search').click()



    def job_info():
        lis = driver.find_elements(By.CSS_SELECTOR, '.job-card-body')
        for li in lis:
            company_name = li.find_element(By.CLASS_NAME, 'company-name').text
            job_name = li.find_element(By.CLASS_NAME, 'job-name').text
            salary = li.find_element(By.CLASS_NAME, 'salary').text
            job_area = li.find_element(By.CLASS_NAME, 'job-area').text
            company_type = li.find_element(By.CLASS_NAME, 'company-tag-list').text
            href = li.find_element(By.CSS_SELECTOR, '.job-card-body a').get_attribute('href')
            print(company_name, job_name, salary, job_area, company_type, href, sep='|')
            dict = {
                '岗位名称': company_type,
                '公司名称': job_name,
                '薪资': salary,
                '工作地点': job_area,
                '公司类型': company_type,
                # '福利': info_desc,
                '详情页链接': href
            }
            csv_writer.writerow(dict)

        driver.find_element(By.CLASS_NAME, 'ui-icon-arrow-right').click()


    for page in range(1, 6):
        time.sleep(1.5)
        job_info()

    driver.quit()
